package com.lottery.ruok.lottrey.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.applozic.mobicomkit.ChatConfig;
import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.activity.GetPacketResultActivity;
import com.lottery.ruok.lottrey.adapter.GetRedPacketAdapter;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.GetRedPacketLogRequest;
import com.lottery.ruok.lottrey.retrofitModule.response.GetRedPacketLogResponse;
import com.lottery.ruok.lottrey.userInfo.UserInfo;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GetRedPacketFragment extends BaseFragment {
    @BindView(R.id.hongbao_list)
    ListView hongbao_list;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;

    private GetRedPacketLogResponse getRedPacketLogResponses;
    private int mIndex = 0;
    private boolean mFirst = true;
    private GetRedPacketAdapter myAdapter;
    private View emptyLayout;

    private void setEmtpyView(){
        if (emptyLayout == null && getView() != null) {
            ViewStub stub = getView().findViewById(R.id.no_net_viewstub);
            View view = stub.inflate();
            if(view != null) {
                emptyLayout = view.findViewById(R.id.no_net_view);
                emptyLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        getList(0);
                    }
                });
            }
        }
    }
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_get_packet, null);

        ButterKnife.bind(this, view);
        init();
        return view;
    }

    private void init() {
        /*BGARefreshViewHolder refreshViewHolder = new BGANormalRefreshViewHolder(getContext(), true);
        swipeRefreshLayout.setRefreshViewHolder(refreshViewHolder);

        swipeRefreshLayout.setDelegate(new BGARefreshLayout.BGARefreshLayoutDelegate() {
            @Override
            public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
                getList(mIndex);
                return true;
            }

            @Override
            public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
                getList(0);
            }
        });
*/
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                mRefreshLayout.setEnableRefresh(true);//启用刷新
                getList(0);

            }
        });
        mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                mRefreshLayout.setEnableLoadMore(true);//启用加载
                getList(mIndex);
            }
        });
        getList(0);

        hongbao_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if(i != 0) {
                    Intent intent = new Intent(getActivity(), GetPacketResultActivity.class);
                    intent.putExtra(ChatConfig.PACKET_GET_ID, getRedPacketLogResponses.getList().getGifts().get(i - 1).getGift_rand_id());
                    intent.putExtra(ChatConfig.PACKET_ID, getRedPacketLogResponses.getList().getGifts().get(i - 1).getGift_id());
                    getContext().startActivity(intent);
                }
            }
        });
    }

    private void getList(final int index){
        RetrofitBase.getRetrofit().create(GetRedPacketLogRequest.class)
                .getResult(UserInfo.getToken(), UserInfo.getUsername(), index+"", "10")
                .compose(RxHelper.<GetRedPacketLogResponse>handleResult())
                .subscribe(new RxSubscribe<GetRedPacketLogResponse>(getActivity()) {
                    @Override
                    protected void _onNext(GetRedPacketLogResponse getRedPacketLogResponse) {
                        if(getActivity() != null){
                            if(index == 0) {
                                getRedPacketLogResponses = getRedPacketLogResponse;
                                myAdapter = new GetRedPacketAdapter(getActivity(), getRedPacketLogResponses);
                                hongbao_list.setAdapter(myAdapter);
                                mRefreshLayout.finishRefresh();
                            } else {
                                getRedPacketLogResponses.getList().getGifts().addAll(getRedPacketLogResponse.getList().getGifts());
                                myAdapter.notifyDataSetChanged();
                                mRefreshLayout.finishLoadMore();
                            }
                            mIndex = index + 10;
                            if(mFirst) {
                                mFirst = false;
                                View headerView = LayoutInflater.from(getActivity()).inflate(R.layout.get_packet_header_view, null);
                                TextView num = headerView.findViewById(R.id.num);
                                num.setText(getRedPacketLogResponses.getList().getNums() + "");
                                TextView lucky = headerView.findViewById(R.id.best_num);
                                lucky.setText(getRedPacketLogResponses.getList().getLucky() + "");
                                TextView total = headerView.findViewById(R.id.total);
                                total.setText(getRedPacketLogResponses.getList().getTotal() + "");
                                hongbao_list.addHeaderView(headerView);
                            }

                            if(getRedPacketLogResponses.getList().getGifts().size() == 0) {
                                setEmtpyView();
                            } else {
                                if(emptyLayout != null){
                                    emptyLayout.setVisibility(View.GONE);
                                }
                            }
                        }
                    }

                    @Override
                    protected boolean showDialog() {
                        return false;
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);

                        if(index == 0){
                            mRefreshLayout.finishRefresh();
                        } else {
                            mRefreshLayout.finishLoadMore();
                        }

                        if(getRedPacketLogResponses == null || getRedPacketLogResponses.getList().getGifts().size() == 0) {
                            setEmtpyView();
                        }
                    }
                });
    }
}